namespace ObjectPersistence {
    using System;
    using System.Text;
    using System.Collections;
    using System.Data.Common;
    using System.Transactions;
    using System.Collections.Generic;
    
    public partial class ProjectImageAssembler : AssemblerBaseUpdatable<KYO.DAL.ProjectImage, ProjectImageCriteria, ProjectImageRowMap, ProjectImageDomainObjectFactory> {
        public ProjectImageAssembler() {
            base.connection = new System.Data.SqlClient.SqlConnection(GlobalUtil.Constants.ConnectionString);
        }
        protected override System.Data.Common.DbConnection InstanceConnection {
            get {
                return this.connection;
            }
        }
        /// <summary>Reads a single object from the database corresponding to the object keys which are passed in</summary>
        public KYO.DAL.ProjectImage ReadSingleByKey(int ProjectImageID) {
            ProjectImageCriteria criteria = new ProjectImageCriteria();
            criteria.Set_ProjectImageID(ProjectImageCriteria.AVAILABLE_OPERATORS.EQUALS, ProjectImageID);
            return this.ReadSingle(criteria);
        }

        internal override void MakePersistent(KYO.DAL.ProjectImage PersistentObject, bool cascade, System.Data.Common.DbCommand comm) {
            if ((PersistentObject.IsNew == false)) {
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) 
                            && (PersistentObject.ChangesMade == false))) {
                    return;
                }
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) == false)) {
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                }
            }

            if ((PersistentObject.IsNew || PersistentObject.ChangesMade)) {
                ProjectImageUpdateFactory updateFactory = new ProjectImageUpdateFactory();
                comm.Parameters.Clear();
                if ((PersistentObject.IsNew == false)) {
                    updateFactory.updateCommandText(PersistentObject, comm);
                    int numRows = comm.ExecuteNonQuery();
                    if ((numRows == 0)) {
                        throw new NoRowsUpdated();
                    }
                }
                else {
                    updateFactory.insertCommandText(PersistentObject, comm);
                    comm.CommandText = (comm.CommandText + "; SELECT SCOPE_IDENTITY();");
                    decimal NewIdentityKey = ((decimal)(comm.ExecuteScalar()));
                    PersistentObject.ProjectImageID = ((int)(NewIdentityKey));
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                    PersistentObject.IsNew = false;
                }
            }
            PersistentObject.ChangesMade = false;

        }

        internal override void Delete(KYO.DAL.ProjectImage PersistentObject, System.Data.Common.DbCommand comm) {
            comm.Parameters.Clear();

            ProjectImageSelectionFactory tempProjectImageSelectionFactory = new ProjectImageSelectionFactory();
            string sql = "DELETE FROM [dbo].[ProjectImage]";
            comm.CommandText = sql;
            tempProjectImageSelectionFactory.getDefaultSelection(PersistentObject, comm);
            comm.ExecuteNonQuery();
        }

        #region Helper Utilities
        protected override ProjectImageRowMap fillRowMap(System.Data.Common.DbDataReader reader) {
            ProjectImageRowMap result = new ProjectImageRowMap();
            result.ProjectImageID = reader.GetInt32(reader.GetOrdinal("dboProjectImageDOTProjectImageID"));
            result.Title = reader.GetString(reader.GetOrdinal("dboProjectImageDOTTitle"));
            result.LinkUrl = reader.GetString(reader.GetOrdinal("dboProjectImageDOTLinkUrl"));
            result.ImageUrl = reader.GetString(reader.GetOrdinal("dboProjectImageDOTImageUrl"));
            result.Odering = reader.GetInt32(reader.GetOrdinal("dboProjectImageDOTOdering"));
            result.Published = reader.GetBoolean(reader.GetOrdinal("dboProjectImageDOTPublished"));
            result.Type = reader.GetString(reader.GetOrdinal("dboProjectImageDOTType"));
            return result;
        }
        internal override void ClearList() {
            GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Clear();
        }

        public override string Abstract_SELECT {
            get {
                return ("SELECT " + ProjectImageAssembler.RawFields);
            }
        }
        protected override string InstanceTable {
            get {
                return "[dbo].[ProjectImage]";
            }
        }
        public const string RawFields = @"[dbo].[ProjectImage].[ProjectImageID] AS dboProjectImageDOTProjectImageID, [dbo].[ProjectImage].[ImageUrl] AS dboProjectImageDOTImageUrl, [dbo].[ProjectImage].[LinkUrl] AS dboProjectImageDOTLinkUrl, [dbo].[ProjectImage].[Odering] AS dboProjectImageDOTOdering, [dbo].[ProjectImage].[Published] AS dboProjectImageDOTPublished, [dbo].[ProjectImage].[Title] AS dboProjectImageDOTTitle, [dbo].[ProjectImage].[Type] AS dboProjectImageDOTType";
        public const string SELECT = @"SELECT [dbo].[ProjectImage].[ProjectImageID] AS dboProjectImageDOTProjectImageID, [dbo].[ProjectImage].[ImageUrl] AS dboProjectImageDOTImageUrl, [dbo].[ProjectImage].[LinkUrl] AS dboProjectImageDOTLinkUrl, [dbo].[ProjectImage].[Odering] AS dboProjectImageDOTOdering, [dbo].[ProjectImage].[Published] AS dboProjectImageDOTPublished, [dbo].[ProjectImage].[Title] AS dboProjectImageDOTTitle, [dbo].[ProjectImage].[Type] AS dboProjectImageDOTType";
        #endregion
    }
}
